#include "pch.h"
#include "CHook2.h"

CHook2* pHook2;

LONG _stdcall PVectoredExcetionHandler2(_EXCEPTION_POINTERS* EInfo) {
	CHookPoint* p = pHook2->m_HPoint.FindPoint((LPVOID)EInfo->ContextRecord->Eip);
	if (p) {
		if (p->m_HookBack(EInfo->ContextRecord)) {
			EInfo->ContextRecord->Eip = (unsigned)(p->m_pCodeFix);
		}
		return EXCEPTION_CONTINUE_EXECUTION;
	}
	else return EXCEPTION_CONTINUE_SEARCH;
}

CHook2::CHook2()
{
	if (pHook2) return;
	pHook2 = this;
	m_HPointLast = &m_HPoint;

	AddVectoredExceptionHandler(1, PVectoredExcetionHandler2);
}

void CHook2::SetHook(unsigned nAddr, short len, HOOKBACK HookBack, unsigned nAddrRet)
{
	DWORD dwOld;
	VirtualProtect((LPVOID)nAddr, len, PAGE_EXECUTE_READWRITE, &dwOld);
	m_HPointLast = m_HPointLast->AddPoint((LPVOID)nAddr, HookBack, len, nAddrRet);

	char* code = (char*)nAddr;
	code[0] = 0xCC;

	VirtualProtect((LPVOID)nAddr, len, dwOld, NULL);
}
